package data.model;

import java.util.TreeSet;

/**
 * High level data structure for an action, has a name and a description
 * 
 * @author Will Home
 *
 */
public class Action implements Comparable<Action> 
{

	/** name of action */
	private String actionName;
	
	/** description of action */
	private String actionDesc;
	
	/** unique ID */
	private final int actID;
	
	/** ID generator */
	private static int idGen = 0;
	
	/** Set of IDs of tasks that perform this action */
	private TreeSet<Integer> tasks;
	
	/** Set of  names of tasks that perform this action */
	private TreeSet<Integer> processors;
	
	/**
	 * constructor
	 * 
	 * @param name - action name
	 */
	public Action(String name)
	{
		actionName = name;
		actID = idGen;
		tasks = new TreeSet<Integer>();
		processors = new TreeSet<Integer>();
		idGen++;
	}
	
	//ACCESSORS
	/**
	 * 
	 * @return action name
	 */
	public String getActName()
	{
		return actionName;
	}
	
	/**
	 * 
	 * @return action description
	 */
	public String getActDesc()
	{
		return actionDesc;
	}
	
	/**
	 * 
	 * @return action ID
	 */
	public int getActID()
	{
		return actID;
	}
	
	/**
	 * 
	 * @return task IDs
	 */
	public TreeSet<Integer> getTasks()
	{
		return tasks;
	}
	
	/**
	 * 
	 * @return processor IDs
	 */
	public TreeSet<Integer> getProcs()
	{
		return processors;
	}
	
	/**
	 * checks whether a particular task is associated with this action
	 * 
	 * @param id - task id
	 * @return true if associated
	 */
	public boolean containsTask(int id)
	{
		return tasks.contains(id);
	}
	
	
	
	//MUTATORS
	/**
	 * assign new action name
	 * 
	 * @param name
	 */
	public void setActName(String name)
	{
		actionName = name;
	}
	
	/**
	 * add action description
	 * 
	 * @param desc
	 */
	public void setActDesc(String desc)
	{
		actionDesc = desc;
	}
	
	/**
	 * Assign new task to this action
	 * 
	 * @param id - task ID
	 * @return success check
	 */
	public boolean addTask(int id)
	{
		return tasks.add(id);
	}
	
	/**
	 * Assign new processor to this action
	 * 
	 * @param id - processor ID
	 * @return success check
	 */
	public boolean addProc(int id)
	{
		return processors.add(id);
	}
	
	/**
	 * remove processor assigned to this action
	 * 
	 * @param id - processor ID
	 * @return success check
	 */
	public boolean removeProc(int id)
	{
		return tasks.remove(id);
	}
	
	/**
	 * remove task assigned to this action
	 * 
	 * @param id - task ID
	 * @return success check
	 */
	public boolean removeTask(int id)
	{
		return tasks.remove(id);
	}
	
	/**
	 * compares actions by action ID
	 * 
	 * @param other
	 * @return 0 if ID is identical, -1 if lower, 1 if higher
	 */
	public int compareTo(Action other)
	{
		if(this.actID == other.getActID())
			return 0;
		else if(this.actID < other.getActID())
			return -1 ;
		else
			return 1;
	}
}
